package com.atguigu.gulimall_product.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.atguigu.gulimall_product.product.dao.CategoryDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall_product.product.entity.CategoryEntity;
import com.atguigu.gulimall_product.product.service.CategoryService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 商品三级分类
 *
 * @author chenzhengping
 * @email 1840854390@gmail.com
 * @date 2023-10-27 16:43:15
 */

@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    CategoryDao categoryDao;
    /**
     * 列表
     */
   //查询出所有分类以及子分类,用树形结构包装起来
    @RequestMapping("/list")
   // @RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        CategoryEntity listWithTree = categoryService.getListWithTree();
        System.out.println(listWithTree);
        return R.ok().put("list",listWithTree.getChildren());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
        System.out.println(category.toString());
        categoryDao.insert(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
        System.out.println(category);
        categoryService.updateById(category);

        return R.ok();
    }
    @RequestMapping("/updateCategory")
    //@RequiresPermissions("product:category:update")
    public R updateCategorys(@RequestBody List<CategoryEntity> list){
        System.out.println("数据测试"+list.toString());
        categoryService.updateBatchById(list);
        return R.ok();
    }
    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        System.out.println("测试数据"+catIds.toString());
		categoryService.deleteByIds(catIds);
        return R.ok();
    }

}
